Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

List Interface → List iterator

List Interface

List iterator

ListIterator in Java Collections

The ListIterator interface is a powerful tool in the Java Collections Framework that extends the functionality of the basic Iterator interface. It specifically applies to iterating over elements in a List implementation, such as ArrayList, LinkedList, and Vector.

Key Features:

Bidirectional Iteration: Unlike Iterator, which only allows forward traversal, ListIterator supports iterating in both directions – forward and backward. This makes it ideal for scenarios where you need to move back and forth within the list. CRUD Operations: ListIterator goes beyond just reading elements. It enables performing all four CRUD (Create, Read, Update, Delete) operations on the list during iteration.

Methods of List Iterator

add(E e): Inserts the specified element into the list (optional operation). ⯁ hasNext(): Similar to Iterator, this method checks if there are more elements ahead in the forward direction. ⯁ next(): Returns the next element in the list and advances the cursor position one step forward. ⯁ nextIndex(): Returns the index of the element that would be returned by the next call to next(). This is useful when you need to know the upcoming element's index. ⯁ hasPrevious():This method checks if there are elements behind the current position in the backward direction. ⯁ previous(): Returns the previous element in the list and moves the cursor position one step backward. ⯁ previousIndex(): Returns the index of the element that would be returned by the next call to previous(). ⯁ remove(): Removes from the list the last element that was returned by next() or previous() (optional operation). This modification affects the list itself. ⯁ set(element): Replaces the last element returned by next() or previous() with the specified element (optional operation). This allows modifying the element within the list during iteration.

Benefits of list iterator:

Modifying elements while iterating: When you need to read elements and potentially change their values within the same loop. Replacing elements: Iterate and replace specific elements with new values. Removing elements conditionally: Traverse the list and remove elements based on certain criteria. Bidirectional processing: Situations where you need to process elements in both directions, like finding the middle element efficiently. Example:
Example of list iterator interface in java collections import java.util.ArrayList; import java.util.ListIterator; public class Main { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(30); numbers.add(40); ListIterator<Integer> iterator = numbers.listIterator(); // Forward iteration and modification while (iterator.hasNext()) { int num = iterator.next(); if (num % 2 == 0) { iterator.set(num * 2); // Double even numbers } } System.out.println("Modified list (forward): " + numbers); // Backward iteration and removal while (iterator.hasPrevious()) { int num = iterator.previous(); if (num > 25) { iterator.remove(); // Remove numbers greater than 25 } } System.out.println("Modified list (backward): " + numbers); } }

Output

Modified list (forward): [20, 40, 60, 80] Modified list (backward): [20]
In conclusion, ListIterator provides a versatile way to interact with List objects in Java. It empowers you to iterate forward and backward, modify elements, and remove them on the fly, making it a valuable tool for various list processing tasks.

Tutorials